<?php
class GroupController extends Controller
{
	public function actionIndex()
	{
		$data['groupList']=Group::model()->findAll(array(
			'select'=>'id,name',
		));
		$this->render('index',$data);
	}	

	public function actionVisit()
	{
		if(!isset($_GET['id']))
			throw new CHttpException(404,'invalid request');

		$gid=(int)$_GET['id'];
		$data['group']=Group::model()->with('topics')->findByPk($gid);

		if(empty($data['group']))
			throw new CHttpException(404,'invalid request');
		
		$data['topicList']=$data['group']->topics;
		$data['admin']=$data['group']->admin;
		if(empty($data['admin']))
			throw new CHttpException(404,'管理员错误');

		if(Yii::app()->user->isGuest)
			$data['joined']=FALSE;
		else
		{
			$uid=Yii::app()->user->id;
			$data['joined']=UserGroup::model()->findByPk(array('uid'=>$uid,'gid'=>$gid));
		}

		$this->render('visit',$data);
	}

	public function actionApply()
	{
		
		if(Yii::app()->user->isGuest)
			$this->redirect('index.php?r=site/login');
		
		if(isset($_POST['ApplyForm']))
		{
			$group=new Group;
			$group->attributes=$_POST['ApplyForm'];
			$group->admin_id=Yii::app()->user->id;
	
			$group->save();
			$this->redirect('index.php?r=group/index');
		}
		$applyForm=new ApplyForm;
		$this->render('apply',array(
			'applyForm'=>$applyForm,
		));
	}

	public function actionList()
	{
		$data['list']=Group::model()->findAll(
			array('select'=>'id,name')
		);
		$this->render('list',$data);
	}

	public function actionJoin()
	{
		//TODO 验证已经加入了
		if(!isset($_GET['gid']))
			throw new CHttpException(404,'invalid request');

		if(Yii::app()->user->isGuest)
			$this->redirect('index.php?r=site/login');

		$userGroup=new UserGroup;
		$userGroup->uid=Yii::app()->user->id;
		$userGroup->gid=(int)$_GET['gid'];
		$userGroup->join_time=date('Y-m-d H:i:s');
		$userGroup->level=0;

		if($userGroup->save())
			$this->redirect('index.php?r=group/visit&id='.$_GET['gid']);
		else
			throw new CHttpException(404,'invalid request');
	}

	public function actionQuit()
	{
		//TODO 验证未加入 
		if(!isset($_GET['gid']))
			throw new CHttpException(404,'invalid request');

		if(Yii::app()->user->isGuest)
			$this->redirect('index.php?r=site/login');
		
		if(UserGroup::model()->deleteByPk(array('uid'=>Yii::app()->user->id,'gid'=>$_GET['gid'])))
			$this->redirect('index.php?r=group/visit&id='.$_GET['gid']);
		else
			throw new CHttpException(404,'invalid request');
	}

	public function actionMember()
	{
		if(!isset($_GET['gid']))
			throw new CHttpException(404,'invalid request');

		$group=Group::model()->with('members')->findByPk($_GET['gid']);
		if(empty($group))
			throw new CHttpException(404,'invalid request');

		$data['group']=$group;
		$data['memberList']=$group->members;

		$this->render('member',$data);
	}

	public function actionAdmin()
	{
		if(!isset($_GET['gid']))
			throw new CHttpException(404,'invalid request');

		$group=Group::model()->findByPk($_GET['gid']);
		if(empty($group))
			throw new CHttpException(404,'小组不存在');

		if(isset($_POST['Group']))
		{
			$group->attributes=$_POST['Group'];
	
			if($group->save())
				$this->redirect($this->createUrl('group/visit',array('id'=>$group->id)));
			else
				throw new CHttpException(404,'更改出错');
		}
		$data['group']=$group;

		$this->render('admin',$data);
	}
}
?>
